/**
 * 深拷贝
 */
export const deepClone = (data) => {
  var type = getType(data);
  var obj;
  if (type === "array") {
    obj = [];
  } else if (type === "object") {
    obj = {};
  } else {
    // 不再具有下一层次
    return data;
  }
  if (type === "array") {
    for (var i = 0, len = data.length; i < len; i++) {
      obj.push(deepClone(data[i]));
    }
  } else if (type === "object") {
    for (var key in data) {
      obj[key] = deepClone(data[key]);
    }
  }
  return obj;
};

function getType(obj) {
  // tostring会返回对应不同的标签的构造函数
  var toString = Object.prototype.toString;
  var map = {
    "[object Boolean]": "boolean",
    "[object Number]": "number",
    "[object String]": "string",
    "[object Function]": "function",
    "[object Array]": "array",
    "[object Date]": "date",
    "[object RegExp]": "regExp",
    "[object Undefined]": "undefined",
    "[object Null]": "null",
    "[object Object]": "object",
  };
  if (obj instanceof Element) {
    return "element";
  }
  return map[toString.call(obj)];
}
// 获取地址栏参数
export const getUrlKey = (name) => {
  return (
    decodeURIComponent(
      (new RegExp(`[?|&]${name}=` + "([^&;]+?)(&|#|;|$)").exec(
        location.href
        // eslint-disable-next-line no-sparse-arrays
      ) || [, ""])[1].replace(/\+/g, "%20")
    ) || null
  );
};
// 两个时间段比较大小
export const compareDate = (d1, d2, need) => {
  const date1 = new Date(Date.parse(d1));
  const date2 = new Date(Date.parse(d2));
  return need
    ? date1.getTime() < date2.getTime()
    : date1.getTime() <= date2.getTime();
};

// 年月日日期补全两位
export const formatDate = (date) => {
  return `${date.getFullYear()}-${
    date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : `0${date.getMonth() + 1}`
  }-${date.getDate() >= 10 ? date.getDate() : `0${date.getDate()}`}`;
};

// 往前推N天
export const getNextDate = (day = 0, allTime) => {
  const dd = new Date();
  dd.setDate(dd.getDate() + day);
  const y = dd.getFullYear();
  const m =
    dd.getMonth() + 1 < 10 ? `0${dd.getMonth() + 1}` : dd.getMonth() + 1;
  const d = dd.getDate() < 10 ? `0${dd.getDate()}` : dd.getDate();
  const h = dd.getHours() < 10 ? `0${dd.getHours()}` : dd.getHours();
  const mm = dd.getMinutes() < 10 ? `0${dd.getMinutes()}` : dd.getMinutes();
  const ss = dd.getSeconds() < 10 ? `0${dd.getSeconds()}` : dd.getSeconds();
  return allTime ? `${y}-${m}-${d} ${h}:${mm}:${ss}` : `${y}-${m}-${d}`;
};

// 当前年
export const getYear = () => {
  const date = new Date();
  return `${date.getFullYear()}`;
};

// 当前月
export const getMonth = () => {
  const date = new Date();
  date.setDate(1);
  let month = parseInt(date.getMonth() + 1);
  if (month < 10) month = `0${month}`;
  return `${date.getFullYear()}-${month}`;
};

// 月初
export const getMonthBegin = () => {
  const date = new Date();
  date.setDate(1);
  let month = parseInt(date.getMonth() + 1);
  let day = date.getDate();
  if (month < 10) month = `0${month}`;
  if (day < 10) day = `0${day}`;
  return `${date.getFullYear()}-${month}-${day}`;
};

// 月末
export const getMonthEnd = () => {
  const date = new Date();
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  month = month < 10 ? `0${month}` : month;
  const day = new Date(year, month, 0);
  return `${year}-${month}-${day.getDate()}`;
};

// 根据星期往前推日期
export const getNextWeek = (nextDate) => {
  const date = new Date(nextDate);
  const num = date.getDay();
  let nextWeek = "";
  switch (num) {
    case 0: // 星期日
      nextWeek = getNextDate((num - 1) * 6);
      break;
    case 1: // 星期一
      nextWeek = getNextDate(num - 1);
      break;
    case 2: // 星期二
    case 3: // 星期三
    case 4: // 星期四
    case 5: // 星期五
    case 6: // 星期六
      nextWeek = getNextDate(-(num - 1));
      break;
    default:
      break;
  }
  return nextWeek;
};

export const getDuration = (myTime) => {
  const days = myTime / 1000 / 60 / 60 / 24;
  const daysRound = Math.floor(days);
  const hours = myTime / 1000 / 60 / 60 - 24 * daysRound;
  const hoursRound = Math.floor(hours);
  const minutes = myTime / 1000 / 60 - 24 * 60 * daysRound - 60 * hoursRound;
  const minutesRound = Math.floor(minutes);
  return `${daysRound}天 ${hoursRound}时 ${minutesRound}分`;
};

// 上传文件处理
export async function handleFiles(files) {
  const fomrData = new FormData();
  let dataList = [];
  if (Array.isArray(files)) {
    dataList = files;
  } else {
    dataList.push(files);
  }
  for (const item of dataList) {
    if (item.raw) {
      fomrData.append("file", item.raw, item.raw.name);
    }
  }
  return fomrData;
}

/* 根据日期获取当前周的 周一和周日 （时间戳）**/
export const getCurrentWeek = (date = new Date()) => {
  const now = typeof date === "object" ? date : new Date(date);
  const nowTime = now.getTime();
  const day = now.getDay();
  const oneDayTime = 24 * 60 * 60 * 1000;

  // 显示周一
  const MondayTime = nowTime - (day - 1) * oneDayTime;
  // 显示周日
  const SundayTime = nowTime + (7 - day) * oneDayTime;

  // 初始化日期时间
  const monday = new Date(MondayTime);
  const sunday = new Date(SundayTime);
  return {
    monday,
    sunday,
  };
};

// export const getImageUrl = (name) => {
//   return new URL(`../assets/blogPhotos/${name}.jpg`, import.meta.url).href;
// }
